import 'package:flutter/material.dart';
import 'package:osc/data/repository/repository.dart';
import 'package:osc/models/entity/osc_base_list_entity.dart';
import 'package:osc/models/entity/osc_user.dart';
import 'package:osc/models/pop_result/pop_result.dart';
import 'package:osc/res/dimens.dart';
import 'package:osc/ui/widgets/common_scaffold.dart';
import 'package:osc/ui/widgets/no_data_widget.dart';
import 'package:osc/ui/widgets/tweet/osc_tweet_pick_user_widget.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

class SearchUserForPick extends StatefulWidget{
  final String keyword;
  const SearchUserForPick({Key key, this.keyword}) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return new SearchUserForPickState();
  }
}

class SearchUserForPickState extends State<SearchUserForPick> with SingleTickerProviderStateMixin{

  HttpRepository httpRepository;
  String pageToken;

  String keyword;

  bool isLoadData = false;

  int userSize = 0;
  List<OscUser> users;

  RefreshController refreshController;

  @override
  void initState() {
    super.initState();
    keyword = widget.keyword;
    refreshController = RefreshController(initialRefresh: true);
    httpRepository = new HttpRepository();
  }

  @override
  void dispose() {
    super.dispose();
    refreshController.dispose();
  }


  @override
  Widget build(BuildContext context) {
    return CommonScaffold(
      title: "网络搜索",
      body: buildBody(),
    );
  }

  buildBody(){
    return SmartRefresher(
      controller: refreshController,
      enablePullUp: true,
      onLoading: loadData,
      onRefresh: refresh,
      child: buildListView(),
    );
  }

  buildListView(){
    return ListView.separated(
      itemBuilder: (BuildContext context, int index) {
        if (userSize == 0) {
          return NoDataWidget();
        } else {
          return buildUserItem(users.elementAt(index));
        }
      },
      separatorBuilder: (BuildContext context, int index) {
        return Divider(height: Dimens.gap_divider_height_smallest,);
      },
      itemCount: isLoadData && userSize == 0? 1 : userSize,
    );
  }

  buildUserItem(OscUser user){
    return OscTweetPickUserWidget(
      user: user,
      onTap: (){
        PopResult result = PopResult(id: PopResult.ID_PICK_FRIEND, success: true, extract: {"friend": user});
        Navigator.pop(context, result);
      },
    );
  }

  refresh() async{
    pageToken = null;
    userSize = 0;
    users?.clear();
    await loadData();
  }

  loadData() async{
    await httpRepository.searchUser(keyword: keyword, pageToken: pageToken).then((resp){
      if(resp != null){
        if(resp.success && resp.result != null){
          OscBaseListResult<OscUser> result = resp.result;
          if(pageToken == null){
            users = result.items;
          } else {
            users.addAll(result.items);
          }
          pageToken = result.nextPageToken;
          userSize = users?.length??0;
          if(result.requestCount > result.responseCount){
            refreshController.loadNoData();
          } else {
            refreshController.loadComplete();
          }
          refreshController.refreshCompleted();
          if (mounted) {
            setState(() {

            });
          }
        }
      }
    });
  }

}